package com.green.web.controller.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.green.common.Constant;
import com.green.common.page.Pagination;
import com.green.model.manage.FunPermission;
import com.green.model.manage.Org;
import com.green.model.manage.Position;
import com.green.service.common.ServiceContext;
import com.green.service.manage.DataPermissionService;
import com.green.service.manage.FunPermissionService;
import com.green.service.manage.ModuleService;
import com.green.service.manage.OrgService;
import com.green.service.manage.PositionService;
import com.green.web.controller.common.BaseController;

/**
 * 组织岗位
 * @author 谷辉
 *
 */
@Controller
@RequestMapping(value = "/funPermission/*")
public class FunPermissionController extends BaseController{

	@Resource
	private FunPermissionService funPermissionService;
	
	@Resource
	private ModuleService moduleService;
	
	@Resource
	private OrgService orgService;
	
	@Resource
	private PositionService positionService;
	@Resource
	private DataPermissionService dataPermissionService;
	/**
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "manager")
	public String manager(ModelMap modelMap) {
		if(ServiceContext.getLoginPerson().getNumber().contains("jmadmin"))
		{
			modelMap.put("NUMBER", Constant.JMSYB_NUMBER);
		}
		if(ServiceContext.getLoginPerson().getNumber().contains("wladmin"))
		{
			modelMap.put("NUMBER", Constant.WLSYB_NUMBER);
		}
		return "manage/FunPermissionManager";
	}
	/**
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "sAdd")
	public String sAdd(ModelMap modelMap) {
		String positionId=getString("positionId");
		String orgId=getString("orgId");
		String displayName=getString("displayName");
		String controUnit=getString("controUnit");

		Position position=positionService.getPositionById(positionId);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("positionId", positionId);	
		modelMap.put("position", position);	
		modelMap.put("displayName", displayName);	
		modelMap.put("controUnit", controUnit);	
		Org org=orgService.getOrgById(orgId);
		modelMap.put("org", org);	
		modelMap.put("dataName", dataPermissionService.getDataPermissionWmConcat(param));
		return "manage/FunPermissionSAdd";
	}
	/**
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "oAdd")
	public String oAdd(ModelMap modelMap) {
		String positionId=getString("positionId");
		String orgId=getString("orgId");
		String displayName=getString("displayName");
		String controUnit=getString("controUnit");
		Position position=positionService.getPositionById(positionId);		
		Org org=orgService.getOrgById(orgId);
		modelMap.put("position", position);	
		modelMap.put("displayName", displayName);	
		modelMap.put("controUnit", controUnit);	

		modelMap.put("org", org);
		return "manage/FunPermissionOAdd";
	}
	
	/**
	 * 
	 * @param modelMap  权限项
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	public void listData(Pagination<Map<String, Object>> page,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("positionId", getString("positionId"));		
		param.put("controUnit", getString("controUnit"));		
		page =moduleService.getModuleListByFunpermission(page, param);
        
		outPrint(response, JSONObject.fromObject(page, getDefaultJsonConfig()));
	}
	/**
	 * 禁用 启用权限
	 * 
	 * @return
	 */
	@RequestMapping("deleteFunPermission")
	@ResponseBody
	public Map<String, Object> deleteFunPermission() {
		String id = getString("id");
		int type = getInt("type");
		String positionId=getString("positionId");
		int count=this.funPermissionService.deleteFunPermission(id, type, positionId);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	/**
	 * 保存 数据查看权限
	 * 
	 * @return
	 */
	@RequestMapping("saveFunPermission")
	@ResponseBody
	public Map<String, Object> saveFunPermission() {
		String positionId = getString("positionId");
		String resourceList = getString("resourceList");
		String moduleId = getString("moduleId");
		
		int count=this.funPermissionService.saveFunPermission(positionId, moduleId, resourceList);
		//岗位查看数据权限
		Map<String, Object> param = new HashMap<String, Object>();		
		param.put("positionId", positionId);	
		param.put("moduleIdList", moduleId);	
		String dataName= dataPermissionService.getDataPermissionWmConcat(param);
		
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		result.put("dataName", dataName);
		return result;
	}
	/**
	 * 查找子节点（初始化和展开子节点时会加载）
	 * 
	 * @param modelMap
	 * @param response
	 */
	@RequestMapping(value = "tree")
	public void getTreeData(ModelMap modelMap, HttpServletResponse response) {
		response.setContentType("text/HTML");		
		String id = getString("id");
		String number = getString("number");
		String tree=this.orgService.getTreeData(id,number);
		this.outPrint(response, tree);
	}

	
	/**
	 * 保存 数据查看权限
	 * 
	 * @return
	 */
	@RequestMapping("getFunPermission")
	@ResponseBody
	public Map<String, Object> getFunPermission() {
		String positionId = getString("positionId");
		String moduleId = getString("moduleId");
		Map<String, Object> param = new HashMap<String, Object>();		
		param.put("positionId", positionId);	
		param.put("moduleIdList", moduleId);				
		String code="";
	    List<FunPermission> fpList= funPermissionService.getFunPermissionList(param);
	    for(FunPermission fp:fpList){
		   code=code+fp.getResource().getCode();
	    }
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("dataName", code);
		return result;
	}
}
